{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- 官方地址：https://redis.io/download\n",
    "- 可视化工具： https://github.com/qishibo/AnotherRedisDesktopManager/\n",
    "- w3c 教程：https://www.w3cschool.cn/redis/redis-install.html"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# linux 离线安装\n",
    "\n",
    "```sh\n",
    "$ wget http://download.redis.io/releases/redis-5.0.5.tar.gz\n",
    "$ tar xzf redis-5.0.5.tar.gz\n",
    "$ cd redis-5.0.5\n",
    "$ make\n",
    "```\n",
    "The binaries that are now compiled are available in the `src` directory. Run Redis with:\n",
    "```sh\n",
    "$ src/redis-server\n",
    "```\n",
    "You can interact with Redis using the built-in client:\n",
    "```sh\n",
    "$ src/redis-cli\n",
    "redis> set foo bar\n",
    "OK\n",
    "redis> get foo\n",
    "\"bar\"\n",
    "```\n",
    "\n",
    "# Ubuntu 在线安装\n",
    "\n",
    "redis 配置文件`redis.conf`在`/etc/redis`目录下\n",
    "在 Ubuntu 系统安装 Redis 可以使用以下命令:\n",
    "\n",
    ">$sudo apt-get update\n",
    "\n",
    ">$sudo apt-get install redis-server\n",
    "\n",
    "启动 Redis\n",
    "\n",
    ">$ redis-server\n",
    "\n",
    "查看 redis 是否启动？\n",
    ">$ redis-cli\n",
    "\n",
    "# 启动&关闭\n",
    "\n",
    "如果是用apt-get或者yum install安装的redis，可以直接通过下面的命令停止/启动/重启redis\n",
    "```sh\n",
    "/etc/init.d/redis-server stop\n",
    "/etc/init.d/redis-server start\n",
    "/etc/init.d/redis-server restart\n",
    "```\n",
    "同：`service redis-server start /stop/ restart`\n",
    "\n",
    "如果是通过源码安装的redis，则可以通过redis的客户端程序`redis-cli`的**shutdown**命令来重启redis\n",
    ">redis-cli -h 127.0.0.1 -p 6379 shutdown\n",
    "\n",
    "如果上述方式都没有成功停止redis，则可以使用终极武器 kill -9"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Windows安装\n",
    "\n",
    "1. 下载地址: https://github.com/MSOpenTech/redis/releases\n",
    "\n",
    "2. 这里我们下载 Redis-x64-xxx.zip压缩包到 C 盘，解压后，将文件夹重新命名为 redis。\n",
    "\n",
    "3. 把 redis 的路径加到系统的环境变量里。\n",
    "\n",
    "4. 打开一个 cmd 窗口 使用 cd 命令切换目录到 C:\\redis 运行：\n",
    ">redis-server.exe redis.windows.conf\n",
    "\n",
    "  或者\n",
    ">redis-server.exe\n",
    "\n",
    "5. 这时候另启一个cmd窗口，原来的不要关闭，不然就无法访问服务端了。\n",
    "  切换到redis目录下运行 \n",
    "  >redis-cli.exe -h 127.0.0.1 -p 6379\n",
    "  \n",
    "  或者使用默认参数\n",
    "  >redis-cli.exe\n",
    "\n",
    "  测试\n",
    "  >set myKey abc\n",
    "\n",
    "  取出键值对 \n",
    "  >get myKey\n",
    "  \n",
    "## 默认服务安装\n",
    "\n",
    "我们不可能每次要用Redis都去开一下临时服务，可不可以跟其它服务一样能够开机自启？当然是可以得，但是有点区别，后续会讲。进入Redis安装包文件下，敲入命令注册服务：\n",
    "\n",
    ">redis-server.exe --service-install redis.windows.conf --loglevel verbose\n",
    "\n",
    "**（一定要把临时服务关闭，否则安装不上）。**\n",
    "\n",
    "安装成功会显示:\n",
    "```\n",
    "# Redis successfully installed as a service.\n",
    "```\n",
    "**启动/暂停/卸载服务**：\n",
    "\n",
    ">redis-server.exe --service-start\n",
    "\n",
    ">redis-server.exe --service-stop\n",
    "\n",
    ">redis-server.exe --service-uninstall\n",
    "\n",
    "## 自定义服务安装  \n",
    "\n",
    "所谓自定义服务安装，就是将服务重命名。进入Redis安装包文件下，注册服务：\n",
    ">redis-server.exe --service-install redis.windows.conf --Service-name RedisServer1 --loglevel verbose\n",
    "\n",
    "备注：通过以上面命令，会在window  Service列表出现\"redisserver1\"服务，但此服务不是启动状态，需要调下面命令启动服务。\n",
    "\n",
    "与默认安装一样，唯一不同的就是在相应安装服务、启动、关闭、卸载服务时需要加上自定义Redis服务名：\n",
    ">redis-server.exe --service-start --Service-name RedisServer1\n",
    "\n",
    ">redis-server.exe --service-stop --Service-name RedisServer1\n",
    "\n",
    ">redis-server.exe --service-uninstall --Service-name RedisServer1\n",
    "\n",
    "通过命令行卸载自定义服务后，电脑重启一下，卸载服务会全部完成。\n",
    "\n",
    "## Redis主从服务安装  \n",
    "\n",
    "主从服务可以实现负载均衡，其实就是把上面Redis安装文件包，拷贝到相应目录，修改主、从服务器配置文件中IP、Port，同时从服务器要指定主服务器 的IP、Port，按照Redis自定义服务安装中命令进行服务安装、服务启动、服务关闭、服务卸载即可使用。\n",
    "\n",
    "我本地主从服务器安装包都还是在D:\\SoftInstall\\Redis-win目录，文件夹master为主服务，文件夹slave1则是从服务，用的都是本地IP:127.0.0.1，生产环境大家可以根据自己实际情况进行设置。\n",
    "\n",
    "**主服务器**（RedisServer1）\n",
    "\n",
    "redis.windows.conf修改如下：\n",
    "\n",
    "- port  6379(默认)，不需要作修改；\n",
    "\n",
    "**从服务器**（RedisServer2）\n",
    "\n",
    "redis.windows.conf修改如下：\n",
    "\n",
    "- port  6380\n",
    "- slaveof  127.0.0.1  6379\n",
    "\n",
    "![pic](../../assets/redis-slave.png)\n",
    "\n",
    "我们用RedisManagement分别进行两个端口连接做客户端，在主服务添加键可以同步到从服务，从服务添加无效。如果从服务没有配置从属关系，我们可以在Redis管理器中对从服务进行配置，打开控制台，输入slaveof 127.0.0.1 6379，效果一样。\n",
    "\n",
    "\n",
    "## 总结\n",
    "\n",
    "注意事项\n",
    "\n",
    "1. Windows使用的这个Redis是64位版本的，32位操作系统的同学就不要折腾了。\n",
    "\n",
    "2. 作为服务运行的Redis配置文件，通常为redis.windows-service.conf，而不是redis.windows.conf。小心不要选错了。\n",
    "\n",
    "我们安装从服务时候，可以不通过配置文件来安装指定端口和指定主机。redis-server.exe --service-install redis.windows.conf --Service-name RedisServer2 --loglevel verbose --port 6380，这样也是指定端口安装。\n",
    "\n",
    "运行**从服务redis-cli.exe**，![pic](../../assets/redis-slave2.png)，这跟RedisManagement一样，也能配从属关系。我之所以没采用，因为我每次运行从服务出不了6380端口（仍是6379），但是我在Redis管理器中是可以连两个端口的服务，固没采用这种方法，网上很多教程说可以，我未实践成功！"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
